<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: order_overview.php 223 2013-09-22 10:20:27Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 223 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 10:20:27 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
?>
<tr>
    <td width="100%">
		<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
			<tr>
				<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
				<td valign="middle" align="right">
					<?php echo draw_form('status', FILENAME_ORDERS, '', 'get').draw_hidden_field('menu', $_GET['menu']); ?>
						<?php echo HEADING_TITLE_STATUS.' '.draw_pull_down_menu('status', array_merge(array(array('id' => '', 'text' => TEXT_ALL_ORDERS)),array(array('id' => '0', 'text' => TEXT_VALIDATING)), $orders_statuses), $_GET['status'], 'onchange="this.form.submit();"'); ?>
					</form>
				</td>
			</tr>
		</table>
	</td>
</tr>
<tr>
	<td>
		<?php
			switch($_GET['action']) {
				case 'delete':
					echo draw_form('orders', FILENAME_ORDERS, get_all_get_params(array ('oID', 'action')).'oID='.$oInfo->orders_id.'&action=deleteconfirm');
					break;
					
				default:
					echo draw_form('status', FILENAME_ORDERS, get_all_get_params(array('action','oID')).'action=multi_status', 'POST');
					if(isset($_GET['page']))
						echo draw_hidden_field('page', $_GET['page']);
					break;
					
			}
		?>
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<script type="text/javascript">
					$(function(){
						$('.all_orders').click(function(){
							if($(this).is(':checked')) {
								$('.single_order').attr('checked', true);
								$(this).closest('tr').addClass('dataTableRowSelected');
							} else {
								$('.single_order').attr('checked', false);
								$(this).closest('tr').removeClass('dataTableRowSelected');
							}
							count_oIDs();
						});
						$('.single_order').click(function(){count_oIDs()});
					});
					function count_oIDs() {
						var oids = $('input.single_order').filter(':checked').length;
						$('#count_oids').html(oids);
					}
				</script>
				<tr>
					<td valign="top">
						<table width="100%" class="dataTable" cellspacing="0" cellpadding="0">
							<tr class="dataTableHeadingRow">
								<td class="dataTableHeadingContent"><input type="checkbox" class="all_orders" /></td>
								<td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CUSTOMERS; ?></td>
								<td class="dataTableHeadingContent" align="right">Nr</td>
								<td class="dataTableHeadingContent" align="center">PDF</td>
								<td class="dataTableHeadingContent" align="center">R-Nr</td>
								<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ORDER_TOTAL; ?></td>
								<td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_DATE_PURCHASED; ?></td>
								<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_STATUS; ?></td>
								<td class="dataTableHeadingContent last" align="right"><?php echo TABLE_HEADING_ACTION; ?></td>
							</tr>
							<?php
							if ($_GET['cID']) {
								$cID = $db->db_prepare($_GET['cID']);
								$orders_query_raw = "SELECT 
															o.*, 
															s.orders_status_name, 
															ot.text AS order_total 
														FROM 
															".TABLE_ORDERS." o 
															LEFT JOIN ".TABLE_ORDERS_TOTAL." ot 
															ON (o.orders_id = ot.orders_id), 
															".TABLE_ORDERS_STATUS." s 
														WHERE 
															o.customers_id = ".$cID." 
														AND 
															(o.orders_status = s.orders_status_id 
															AND 
																s.language_id = '".$_SESSION['languages_id']."' 
															AND 
																ot.class = 'ot_total') 
														OR 
															(o.orders_status = '0' 
															AND 
																ot.class = 'ot_total' 
															AND 
																s.orders_status_id = '1' 
															AND 
																s.language_id = '".$_SESSION['languages_id']."') 
														ORDER BY 
															orders_id DESC";
							
							} elseif ($_GET['status'] == '0') {
								$orders_query_raw = "SELECT 
															o.*, 
															ot.text AS order_total 
														FROM 
															".TABLE_ORDERS." o 
															LEFT JOIN ".TABLE_ORDERS_TOTAL." ot ON (o.orders_id = ot.orders_id) 
														WHERE 
															o.orders_status = '0' 
														AND 
															ot.class = 'ot_total' 
														ORDER BY 
															o.orders_id DESC";
							
							} elseif($_GET['status']) {
								$status = $db->db_prepare($_GET['status']);
								$orders_query_raw = "SELECT 
															o.*, 
															s.orders_status_name, 
															ot.text AS order_total 
														FROM 
															".TABLE_ORDERS." o 
															LEFT JOIN ".TABLE_ORDERS_TOTAL." ot ON (o.orders_id = ot.orders_id), 
															".TABLE_ORDERS_STATUS." s 
														WHERE 
															o.orders_status = s.orders_status_id 
														AND 
															s.language_id = '".$_SESSION['languages_id']."' 
														AND 
															s.orders_status_id = ".$status." 
														AND 
															ot.class = 'ot_total' 
														ORDER BY 
															o.orders_id DESC";
							
							} else {
								$orders_query_raw = "SELECT 
															o.*, 
															s.orders_status_name, 
															ot.text AS order_total 
														FROM 
															".TABLE_ORDERS." o 
															LEFT JOIN ".TABLE_ORDERS_TOTAL." ot 
															ON (o.orders_id = ot.orders_id), 
															".TABLE_ORDERS_STATUS." s 
														WHERE 
															(s.orders_status_id = o.orders_status
																AND 
															s.language_id = '".$_SESSION['languages_id']."' 
																AND 
															ot.class = 'ot_total') 
														OR 
															(o.orders_status = '0' 
																AND 
															ot.class = 'ot_total' 
																AND 
															s.orders_status_id = '1' 
																AND 
															s.language_id = '".$_SESSION['languages_id']."'
															) 
														ORDER BY 
															o.orders_id DESC";
							}
							
							$query_orders = page_break_create($_POST['per_site'], $_GET['page'], $orders_query_raw, 'orders.php', (isset($_GET['status']) ? 'status='.$_GET['status'] : ''));
							$orders = $query_orders['query'];
							
							if($orders->_numOfRows) {
								$rows = 1;
								while (!$orders->EOF) {
									if (((!$_GET['oID']) || ($_GET['oID'] == $orders->fields['orders_id'])) && (!$oInfo))
										$oInfo = new objectInfo($orders->fields);
							
									$rech_nr = '-';
									$pdf_ico = '-';
									$email = '';
									if(get_pdf_nr($orders->fields['orders_id'], true) > '0') {
										$rech_nr = '<b>'.get_pdf_nr($orders->fields['orders_id'], true).'</b>';
										$pdf_ico = image('images/icons/document-pdf-text.png', 'PDF');
										if(get_pdf_nr($orders->fields['orders_id'], false, false, true) > 0)
											$email = image('images/icons/mail--arrow.png', 'Email');
									}
								
									if ((is_object($oInfo)) && ($orders->fields['orders_id'] == $oInfo->orders_id)) {
										echo '<tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'pointer\'">'."\n";
								    } else {
								    	if ($rows % 2 == 0)
								    		$f = 'dataTableRow';
								    	else
								    		$f = '';
								        echo '<tr class="'.$f.'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''.$f.'\'">'."\n";
								    }
									$onclick = 'onclick="document.location.href=\''.href_link(FILENAME_ORDERS, get_all_get_params(array('oID')).'oID='.$orders->fields['orders_id']).'\'"';
								?>
									<td>
										<input class="single_order" type="checkbox" name="oids[]" value="<?php echo $orders->fields['orders_id'] ?>" />
									</td>
									<td <?php echo $onclick ?>>
										<?php echo '<a href="'.href_link(FILENAME_ORDERS, get_all_get_params(array('oID', 'action')).'oID='.$orders->fields['orders_id'].'&action=edit').'">
											'.$orders->fields['customers_name'].'</a>'; ?>
									</td>
						      <td align="right" <?php echo $onclick ?>><?php echo build_order_id($orders->fields['date_purchased'], $orders->fields['orders_id'], 'confirm'); ?></td>
									<td align="center" <?php echo $onclick ?>><?php echo $pdf_ico.$email; ?></td>
									<td align="center" <?php echo $onclick ?>><?php echo $rech_nr; ?></td>
						            <td align="right" <?php echo $onclick ?>><?php echo strip_tags($orders->fields['order_total']); ?></td>
						            <td align="center" <?php echo $onclick ?>><?php echo datetime_short($orders->fields['date_purchased']); ?></td>
						            <td align="right" <?php echo $onclick ?>>
						            	<?php if($orders->fields['orders_status']!='0') { echo $orders->fields['orders_status_name']; }else{ echo '<font color="#FF0000">'.TEXT_VALIDATING.'</font>';}?>
						            </td>
						            <?php if (AFTERBUY_ACTIVATED=='true') { ?>
						            <td align="right" <?php echo $onclick ?>><?php
						
										if ($orders->fields['afterbuy_success'] == 1) {
											echo $orders->fields['afterbuy_id'];
										} else {
											echo 'TRANSMISSION_ERROR';
										}
									?></td>
						            <?php } ?>
						            <td align="right" class="last" <?php echo $onclick ?>><?php
						            	if ( (is_object($oInfo)) && ($orders->fields['orders_id'] == $oInfo->orders_id) ) {
						            		echo image(DIR_WS_IMAGES.'icon_arrow_right.gif', '');
						            	} else {
						            		echo '<a href="'.href_link(FILENAME_ORDERS, get_all_get_params(array('oID','dl','gp','ms')).'oID='.$orders->fields['orders_id']).'">'.image(DIR_WS_IMAGES.'icon_info.gif', IMAGE_ICON_INFO).'</a>';
						            		echo ' <a href="'.href_link(FILENAME_ORDERS, get_all_get_params(array('oID','dl','gp','ms')).'oID='.$orders->fields['orders_id']).'&action=edit">'.image(DIR_WS_IMAGES.'icon_edit.gif', IMAGE_ICON_ORDER_EDIT).'</a>';
						            	}	?>
						            </td>
								</tr>
							<?php $rows++;
							$orders->MoveNext();
						} 
					} else
						echo '<tr><td colspan="10" align="center"><em>Es gibt derzeit noch keine Bestellungen.</em></td></tr>';
					?>
					</table>
				</td>
				<?php
				$heading = array();
				$contents = array();
				switch ($_GET['action']) {
					case 'delete' :
						$heading[] = array ('text' => '<b>'.TEXT_INFO_HEADING_DELETE_ORDER.'</b>');
						$contents[] = array ('text' => '<br />'.draw_checkbox_field('restock').' '.TEXT_INFO_RESTOCK_PRODUCT_QUANTITY);
				
						if(defined('TABLE_PAYPAL')){
							$db_installed = false;
							$tables = $db->db_query("SHOW TABLES LIKE '".TABLE_PAYPAL."'");
							if($tables->_numOfRows)
								$db_installed=true;
							
							if ($db_installed==true){
								$query = $db->db_query("SELECT * FROM ".TABLE_PAYPAL." WHERE order_id = '".$oInfo->orders_id."'");
								if($query->_numOfRows)
									$contents[] = array ('text' => '<br />'.draw_checkbox_field('paypaldelete').' '.TEXT_INFO_PAYPAL_DELETE);
							}
						}
						$contents[] = array ('align' => 'center', 'text' => '<button type="submit" class="button">'. BUTTON_DELETE .'</button> <a class="button" href="'.href_link(FILENAME_ORDERS, get_all_get_params(array ('oID', 'action')).'oID='.$oInfo->orders_id).'">'.BUTTON_CANCEL.'</a>');
						break;
				
					default :
						if (is_object($oInfo)) {
							$heading[] = array ('text' => '<b>['.$oInfo->orders_id.']&nbsp;&nbsp;'.datetime_short($oInfo->date_purchased).'</b>');
				
							$contents[] = array ('align' => 'center', 'text' => '<a class="button" href="'.href_link(FILENAME_ORDERS, get_all_get_params(array ('oID', 'action', 'print_oID')).'oID='.$oInfo->orders_id.'&action=edit').'">'.BUTTON_EDIT.'</a> <a class="button" href="'.href_link(FILENAME_ORDERS, get_all_get_params(array ('oID', 'action', 'print_oID')).'oID='.$oInfo->orders_id.'&action=delete').'">'.BUTTON_DELETE.'</a><br />');
							
							if($oInfo->payment_method == 'pi_clickandbuy' OR $oInfo->payment_method == 'pi_clickandbuy_recurring') {
								$contents[] = array ('align' => 'center', 'text' => '<a class="button cab_button" href="pi_clickandbuy_details.php?oID='.$oInfo->orders_id.'">ClickandBuy Details</a><br />');		
							}
							
							$contents[] = array ('align' => 'center', 'text' => '<a class="button" href="'.href_link(FILENAME_ORDERS, get_all_get_params(array ('oID', 'action')).'oID='.$oInfo->orders_id.'&action=send&sta=1&stc=0').'">'.'An Admin Erneut Versenden'.'</a><br />');
							$contents[] = array ('align' => 'center', 'text' => '<a class="button" href="'.href_link(FILENAME_ORDERS, get_all_get_params(array ('oID', 'action')).'oID='.$oInfo->orders_id.'&action=send&sta=0&stc=1').'">'.'An Kunden Erneut Versenden'.'</a><br />');
				
							if (AFTERBUY_ACTIVATED == 'true') {
								$contents[] = array ('align' => 'center','text' => '<a class="button" href="'.href_link(FILENAME_ORDERS, get_all_get_params(array ('oID', 'action')).'oID='.$oInfo->orders_id.'&action=afterbuy_send').'">'.BUTTON_AFTERBUY_SEND.'</a>');
				
							}
							$contents[] = array ('align' => 'center', 'text' => '<hr size="1" style="color:#ccc" />');
				
							$contents[] = array ('text' => '<br />'.TEXT_DATE_ORDER_CREATED.' '.date_short($oInfo->date_purchased));
							if (not_null($oInfo->last_modified))
								$contents[] = array ('text' => TEXT_DATE_ORDER_LAST_MODIFIED.' '.date_short($oInfo->last_modified));
				
							if ($oInfo->payment_method !='' && $oInfo->payment_method != 'no_payment') {
								include(DIR_FS_CATALOG.'lang/'.$_SESSION['language'].'/modules/payment/'.$oInfo->payment_method.'.php');
						 		$payment_method = constant(strtoupper('MODULE_PAYMENT_'.$oInfo->payment_method.'_TEXT_TITLE'));
								$contents[] = array ('text' => '<br /><u>'.TEXT_INFO_PAYMENT_METHOD.'</u><br />&nbsp;'.$payment_method);
							}
							$order = new order($oInfo->orders_id);
							$contents[] = array ('text' => '<br /><br /><u>'.sizeof($order->products).' '.PRODUCTS.'</u>');
							$c = '<table width="100%" colspan="5">';
							for ($i = 0; $i < sizeof($order->products); $i++) {
								if(!empty($order->products[$i]['image']))
									$img = '<img src="../'.$order->products[$i]['image'].'" alt="" />';
								else
									$img = '';
								$c .= '<tr><td width="1">'.$img.'</td><td valign="top">'.$order->products[$i]['qty'].'x '.$order->products[$i]['name'].'</td></tr>';
				
								if (sizeof($order->products[$i]['attributes']) > 0) {
									for ($j = 0; $j < sizeof($order->products[$i]['attributes']); $j ++) {
										$c .= '<em class="fs85"> - '.$order->products[$i]['attributes'][$j]['option'].': '.$order->products[$i]['attributes'][$j]['value'].'</em><br />';
									}
								}
							}
							$c .= '</table>';
							$contents[] = array ('text' => $c);
							$heading_multi_status[]	= array ('text' => '<b><span id="count_oids">0</span> '.HEADING_MULTI_STATUS.'</b>');
							$content_multi_order_status[] = array ('text' => draw_pull_down_menu('status', array_merge(array(array('id' => '', 'text' => TEXT_STATUS)),array(array('id' => '0', 'text' => TEXT_VALIDATING)), $orders_statuses)));
							$content_multi_order_status[] = array ('text' => draw_checkbox_field('notify', 'on').ENTRY_NOTIFY_CUSTOMER);
							$content_multi_order_status[] = array ('text' => draw_checkbox_field('notify_comments', 'on').ENTRY_NOTIFY_COMMENTS);
							$content_multi_order_status[] = array ('text' => TABLE_HEADING_COMMENTS.'<br>'.draw_textarea_field('comments', '', 24, 5, $_GET['comments'],'',false).'<br>');
							$content_multi_order_status[] = array ('align' => 'left', 'text' => '<div align="center"><button type="submit" class="button">'.BUTTON_SEND.'</button></div>');
						}
						break;
				}
				
				if ((not_null($heading)) && (not_null($contents))) {
					echo '<td width="25%" valign="top" class="border">'."\n";
					$box = new box;
					echo $box->infoBox($heading, $contents).'<br />';
					
					if(!empty($heading_multi_status) && !empty($content_multi_order_status))
						echo $box->infoBox($heading_multi_status, $content_multi_order_status).'<br />';
	
					echo '</td>'."\n";
				}
				?>
			</tr>
		</table>
	</form>	
	<table width="100%">
		<tr>
			<td colspan="5">
				<table border="0" cellspacing="0" cellpadding="10" width="100%">
					<tr class="page_break">
						<td class="smallText" valign="top" width="33.3%"><?php echo 'Angezeigt werden '.$query_orders['from'].' bis '.$query_orders['to'].' (von insgesamt '.$query_orders['total'].' Bestellungen)'; ?></td>
						<td class="smallText" align="center" width="33.3%"></td>
						<td class="smallText" align="right" width="33.3%">
							Bestellungen pro Seite: <?php echo $query_orders['page_break']->perSiteDropdown($_GET['page']); ?>
						</td>
					</tr>
					<tr>
						<td align="center" colspan="3"><?php echo '<br />'.$query_orders['links']; ?></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</td>
</tr>